home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / handson / java / loops / frame1.java next >
Encoding:
Java Source  |  1997-02-07  |  3.3 KB  |  148 lines

  1. /*
  2.   PC Plus Sample program.
  3.   Java Loops
  4. */
  5.  
  6. import java.awt.*;
  7.  
  8. public class Frame1 extends Frame {
  9.  
  10.  
  11.     void button1_Clicked(Event event) {
  12.     // for loop
  13.         list1.clear();
  14.         for( int i = 1; i < 11; i++ )
  15.            list1.addItem( "for loop: " + i ); 
  16.         list1.addItem( "End of for Loop" );
  17.     }
  18.     
  19.     void button2_Clicked(Event event) {
  20.     // while loop
  21.         int i = 1;
  22.         list1.clear();
  23.         while( i < 11 )
  24.             list1.addItem( "while loop: " + i++ ); 
  25.         list1.addItem( "End of while Loop" );
  26.  
  27.     }
  28.  
  29.     void button3_Clicked(Event event) {
  30.     // do..while loop
  31.         int i = 1;
  32.         list1.clear();
  33.         do
  34.             list1.addItem( "do..while loop: " + i++ ); 
  35.         while ( i < 11 );
  36.         list1.addItem( "End of do..while Loop" );
  37.  
  38.     }
  39.  
  40.     void Open_Action(Event event) {
  41.         //{{CONNECTION
  42.         // Action from Open... Show the OpenFileDialog
  43.         //}}
  44.     }
  45.  
  46.     void About_Action(Event event) {
  47.         //{{CONNECTION
  48.         // Action from About Create and show as modal
  49.                 //}}
  50.     }
  51.  
  52.     void Exit_Action(Event event) {
  53.         //{{CONNECTION
  54.         // Action from Exit Create and show as modal
  55.                 //}}
  56.     }
  57.  
  58.     public Frame1() {
  59.  
  60.         //{{INIT_CONTROLS
  61.         setLayout(null);
  62.         addNotify();
  63.         resize(insets().left + insets().right + 405,insets().top + insets().bottom + 305);
  64.         list1 = new java.awt.List(0,false);
  65.         add(list1);
  66.         list1.reshape(insets().left + 12,insets().top + 12,378,175);
  67.         button1 = new java.awt.Button("for loop");
  68.         button1.reshape(insets().left + 24,insets().top + 204,102,46);
  69.         add(button1);
  70.         button2 = new java.awt.Button("while loop");
  71.         button2.reshape(insets().left + 156,insets().top + 204,102,46);
  72.         add(button2);
  73.         button3 = new java.awt.Button("do..while loop");
  74.         button3.reshape(insets().left + 276,insets().top + 204,102,46);
  75.         add(button3);
  76.         setTitle("A Basic Application");
  77.         //}}
  78.  
  79.         //{{INIT_MENUS
  80.         //}}
  81.     }
  82.  
  83.     public Frame1(String title) {
  84.         this();
  85.         setTitle(title);
  86.     }
  87.  
  88.     public synchronized void show() {
  89.         move(50, 50);
  90.         super.show();
  91.     }
  92.  
  93.     public boolean handleEvent(Event event) {
  94.         if (event.id == Event.WINDOW_DESTROY) {
  95.             hide();         // hide the Frame
  96.             dispose();      // free the system resources
  97.             System.exit(0); // close the application
  98.             return true;
  99.         }
  100.         if (event.target == button1 && event.id == Event.ACTION_EVENT) {
  101.             button1_Clicked(event);
  102.             return true;
  103.         }
  104.         if (event.target == button2 && event.id == Event.ACTION_EVENT) {
  105.             button2_Clicked(event);
  106.             return true;
  107.         }
  108.         if (event.target == button3 && event.id == Event.ACTION_EVENT) {
  109.             button3_Clicked(event);
  110.             return true;
  111.         }
  112.         return super.handleEvent(event);
  113.     }
  114.  
  115.     public boolean action(Event event, Object arg) {
  116.         if (event.target instanceof MenuItem) {
  117.             String label = (String) arg;
  118.             if (label.equalsIgnoreCase("Open...")) {
  119.                 Open_Action(event);
  120.                 return true;
  121.             } else
  122.             if (label.equalsIgnoreCase("About")) {
  123.                 About_Action(event);
  124.                 return true;
  125.             } else
  126.                 if (label.equalsIgnoreCase("Exit")) {
  127.                     Exit_Action(event);
  128.                     return true;
  129.             }
  130.         }
  131.         return super.action(event, arg);
  132.     }
  133.  
  134.     static public void main(String args[]) {
  135.         (new Frame1()).show();
  136.     }
  137.  
  138.     //{{DECLARE_CONTROLS
  139.     java.awt.List list1;
  140.     java.awt.Button button1;
  141.     java.awt.Button button2;
  142.     java.awt.Button button3;
  143.     //}}
  144.  
  145.     //{{DECLARE_MENUS
  146.     //}}
  147. }
  148.